package com.juno.cafenoa.mypage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.gcm.GCMRegistrar;
import com.juno.cafenoa.Constants;
import com.juno.cafenoa.GCMIntentService;
import com.juno.cafenoa.R;
import com.juno.cafenoa.TopNavigatorView;
import com.juno.cafenoa.member.AccountChangeActivity;
import com.juno.cafenoa.util.CafeNoaPreference;
import com.juno.ui.uitableview.UITableView;
import com.juno.ui.uitableview.UITableView.ClickListener;
import com.juno.ui.uitableview.model.BasicItem;
import com.juno.ui.uitableview.model.ViewItem;

public class MyPage extends Activity {
	UITableView mTableView = null;
	TopNavigatorView navi = null;
	Context mContext = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mypage);
		navi = (TopNavigatorView) findViewById(R.id.navi_mypage);
		mTableView = (UITableView) findViewById(R.id.mypageTableView);
		navi.init("MY PAGE");
		mContext = this;
		setListItem();
	}
	
	protected UITableView getUITableView(){
		return mTableView;
	}

	private class CustomClickListener implements ClickListener {

		@Override
		public void onClick(int index) {
			switch(index){
			case 0:
				//포인트 조회. dialog 띄움
				AlertDialog.Builder builder = new AlertDialog.Builder(MyPage.this);
				builder.setTitle("내 포인트");
				builder.setMessage(CafeNoaPreference.getUserPoint(mContext) + "포인트" );
				builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				
				AlertDialog dialog = builder.create();
				dialog.show();
				break;
			case 1:
				//계정설정. 인텐트 전환
				Intent cIntent = new Intent(mContext, AccountChangeActivity.class);
				mContext.startActivity(cIntent);
				break;
			case 2:
				Intent lIntent = new Intent(mContext, MyOrderList.class);
				mContext.startActivity(lIntent);
				break;
			case 3:
				// 패스워드설정. 인텐트 전환
				Intent pIntent = new Intent(mContext, PasswordActivity.class);
				
				if(CafeNoaPreference.getUserPassword(mContext).equalsIgnoreCase("")){
					//미리 설정된 패스워드가 없다면.
					pIntent.putExtra(Constants.USER_PASSWORD_MODE_SET, Constants.PASSWORD_SET);
				}else {
					//미리 설정된 패스워드가 있다면. 재설정
					pIntent.putExtra(Constants.USER_PASSWORD_MODE_SET, Constants.PASSWORD_RESET);
				}
				mContext.startActivity(pIntent);
				break;
			}
		}

	}
	
	private void setListItem(){
		LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		CustomClickListener clickListener = new CustomClickListener();
		
		mTableView.setClickListener(clickListener);

		mTableView.addBasicItem("포인트 조회");
		mTableView.addBasicItem("계정 설정");
		mTableView.addBasicItem("주문내역 조회");
		mTableView.addBasicItem("패스워드 설정");
		//5번째 뷰 정의
		RelativeLayout v = (RelativeLayout) mInflater.inflate(R.layout.mypage_version_row, null);
		TextView version = (TextView) v.findViewById(R.id.version_number);
		version.setText(Constants.CAFE_NOA_VERSION);
		//6번째 뷰 정의.
		RelativeLayout v2 = (RelativeLayout) mInflater.inflate(R.layout.mypage_push_row, null);
		//push서비스를 받을것인지 안받을것인지 알리기위한 check box.
		CheckBox check = (CheckBox) v2.findViewById(R.id.getPushMessage);
		check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if(arg1 != CafeNoaPreference.getPushGet(mContext)){
					CafeNoaPreference.setPushGet(mContext, arg1);
				}
				
				if(arg1){
					GCMRegistrar.register(mContext, GCMIntentService.PROJECT_ID);
					String deviceToken = GCMRegistrar.getRegistrationId(mContext);
					if(!deviceToken.equalsIgnoreCase("")){
						GCMRegistrar.setRegisteredOnServer(mContext, true);
					}
				} else {
					GCMRegistrar.unregister(mContext);
				}
			}
		});
		//5번째뷰 추가
		ViewItem vItem = new ViewItem(v2);
		vItem.setClickable(false);
		mTableView.addViewItem(vItem);
		
		//6번째 뷰 추가.
		ViewItem viewItem = new ViewItem(v);
		viewItem.setClickable(false);
		mTableView.addViewItem(viewItem);
		
		mTableView.commit();
	}

}
